<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title>APP</title>
		<!--<link rel="stylesheet" type="text/css" href="../css/api.css" />-->
		<link rel="stylesheet" type="text/css" href="../css/aui.css" />
		<!--<link rel="stylesheet" type="text/css" href="../css/common.css" />-->
		<style>
		
		body{
		height:100%;
		overflow-y:hidden;		
		}		
			#contain {
				left: 5px;
				top: 5px;
				margin: 0;
				width: 350px;
				height: 400px;
				position: absolute;
				background-color: black;
				border: 1px solid red;
			}
			/*垂直按钮*/
			.aui-btn-tab {
				width: 100%;
				background-color: #fff;
				display: box;
				display: -webkit-box;
				box-sizing: border-box;
				font-size: 12px;
				height: 55px;
			}
			.aui-btn-tab li {
				position: relative;
				text-align: center;
				color: #666;
				width: 100%;
				box-flex: 1;
				-webkit-box-flex: 1;
				box-sizing: border-box;
			}
			.aui-btn-tab li {
				font-size: 12px;
				margin-bottom: 0;
			}
			.aui-btn-tab .aui-iconfont {
				font-size: 22px;
				color: #666;
				padding-top: 5px;
				height: 30px;
				line-height: 30px;
				display: inline-block;
			}
			/*.aui-btn-tab li .aui-iconfont:active {
			 color: #16a085;
			 background-color: #bdc3c7;
			 }*/
			.aui-btn-tab li:active {
				color: #f1c40f;
				background-color: #bdc3c7;
			}
			.aui-btn-tab li:active p {
				color: #f1c40f;
				background-color: #bdc3c7;
			}
			.aui-btn-tab li:active span {
				color: #f1c40f;
				background-color: #bdc3c7;
			}
		</style>
	</head>
	<body>
		<canvas id='contain'></canvas>
		<footer class="aui-nav" id="footer">
			<ul class="aui-btn-tab">
				<!--<li class="active-warning" id="tabbar1" tapmode onclick="Interpolation();">
				<span class="aui-iconfont aui-icon-move1"></span>
				<p>
				插值
				</p>
				</li>-->
				<li id="tabbar2" tapmode onclick="OpenList();">
					<span class="aui-iconfont aui-icon-file"></span>
					<p>
						记录
					</p>
				</li>
				<li id="tabbar3" tapmode onclick="Clear();">
					<span class="aui-iconfont aui-icon-cut"></span>
					<p>
						清除
					</p>
				</li>
				<li id="tabbar4" tapmode onclick="Save(1)">
					<span class="aui-iconfont aui-icon-upload"></span>
					<p>
						保存
					</p>
				</li>
				<li id="tabbar4" tapmode onclick="Save(2,Upload);">
					<span class="aui-iconfont aui-icon-upload"></span>
					<p>
						用作模板
					</p>
				</li>
				<li id="tabbar4" tapmode onclick="Verify();">
					<span class="aui-iconfont aui-icon-safe"></span>
					<p>
						认证
					</p>
				</li>
			</ul>
		</footer>
	</body>
	<script type="text/javascript" src="../script/api.js"></script>
	<script type="text/javascript" src="../script/sign.js"></script>
	<script type="text/javascript" src="../script/APICloud-rest-SHA1.js"></script>
	<!--<script type="text/javascript" src="../script/picture.js"></script>-->
	<script type="text/javascript" src="../script/debug.js"></script>
	<script type="text/javascript">
		var APPID = "A6996268375072";
		var APPKEY = "DF2513D5-2329-E8B0-7064-A9D8D701D282";
		var sig = null;
		var userName = "胡华成";
		var userType = "_T_";
		var uid = null;
		var bSave = false;
		var imgname = null;
		var txtname = null;
		
		var kind = 2;//0-基本，1-签名，2-模板
		var Dirs = [".","data", "template"];
		var models=["SignData","SignData","SignTemplate"];//模型数据		
		var tpllist=[];//上传服务器的模板签名--可以使用做模板的	
		var siglist=[];//上传服务器的认证签名--进行过认证的	
		var temp={};//临时文件
		temp.sig=[];//待认证签名数据
		temp.tpl=[];//模板签名数据
		
		//显示本机签名记录
		function OpenList() {
			api.openWin({
	            name: 'siglist',
	            url: 'siglist_win.html',
	            vScrollBarEnabled:false,
	            delay:200
            });
		}
				
		function Verify() {
			Save(1,function(){
				alert("功能待开启");			
			});
		}
		//差值
		function Interpolation() {
			var str = sig.interpolation(10);
			alert(str);
		}
		//重新绘制
		function Redraw() {
			sig.redraw(10);
		}

		//清除
		function Clear() {
			sig.clear();
			//sig.drawGrid();
		}
		
		function Save(k,callback){
			kind=k;
			if (bSave)
				return;
			bSave = true;
			var filename1 = userName + userType + sig.sigtime() + ".jpg";
			var filename2 = userName + userType + sig.sigtime() + ".txt";
			imgname=filename1;
			txtname=filename2;			
			var path1 = "fs://" + Dirs[kind]+"/"+filename1;
			var path2 = "fs://" + Dirs[kind]+"/"+filename2;
			if (sig.getCount() <= 0) {				
				bSave = false;
				api.toast({
					msg : '暂无签名数据，请重新输入！'
				}, 1000);
				return;
			}
			api.writeFile({
				path : path1,
				data : sig.UrlString()
			}, function(ret, err) {				
				api.toast({
					msg : JSON.stringify(filename1)
				}, 1000);
				api.writeFile({
	                path:path2,
	                data:sig.toString(),
                },function(ret,err){
                	bSave=false;
                	if(typeof callback=='function')
                		callback();
                });				
			});		
		}
		
		//上传
		function Upload() {
			if (bSave)
				return;
			bSave = true;
			var client = new Resource(APPID, APPKEY);
			var model = client.Factory(models[kind]);
			var upfile = client.Factory(models[kind]);
			upfile.save({
				datafile : {
					isFile : true,
					path : "fs://" + Dirs[kind]+"/"+txtname,
					values : {
						filename : txtname
					}
				},
				imgfile : {
					isFile : true,
					path : "fs://" + Dirs[kind]+"/"+imgname,
					values : {
						filename : imgname
					}
				},
				data : '',
				filename : txtname,
				signame : userName,
				kind : 0,
				userid : uid
			}, function(ret, err) {
				bSave = false;
				upfile = null;
				model = null;
				client = null;
				if(ret)
				{										
					tpllist.push(ret);
					api.writeFile({
	                    path:'fs://template.json',
	                    data:JSON.stringify(tpllist)
                    },function(ret,err){
                    alert("上传成功！");                    	
                    });					
				}
				else
				{
					api.readFile({
	                    path:'fs://temp_tpl.json'
                    },function(ret,err){
                    	if(ret)
                    		temp.tpl=JSON.parse(ret);
                    });
                                        
					api.writeFile({
	                    path:'fs://template.json',
	                    data:JSON.stringify(tpllist)
                    },function(ret,err){
                    alert("上传成功！");                    	
                    });						
					console.log(err);
				}
				
			});
		}

		apiready = function() {
			uid = $api.getStorage("uid");
			userName = api.pageParam.signame || '测试用户';			
			var h = api.frameHeight;
			var w = api.frameWidth;			
			var can = document.getElementById("contain");
			var foot = document.getElementById("footer");
			var pos = getOffset(foot);
			
			can.style.top = "0px";
			can.style.left = "0px";
			can.style.width = w - 0 + "px";
			can.style.height = h - 0 - pos.h + "px";
						
			sig = new Sign("contain", 360, 450);
			sig.Init();
						
			var device = {};
			device.deviceName = api.deviceName;
			device.deviceModel = api.deviceModel;
			device.deviceId = api.deviceId;
			device.systemType = api.systemType;
			device.systemVersion = api.systemVersion;			
			sig.setDevice(device);
			
			var headHight = api.winHeight - api.frameHeight;
			window.addEventListener("orientationchange", function() {
				var degree = window.orientation;
				var w = api.winWidth;
				var h = api.winHeight;
				var w1 = api.frameWidth;
				var h1 = api.frameHeight;
				if (w > h) {
					var t = w;
					w = h;
					h = t;
				}				
				can.style.width = w - 0 + "px";
				can.style.height = h - 0 - pos.h + "px";
				sig.Init();
				sig.degree = degree;
				if (degree == 0) {
					can.style.width = w - 0 + "px";
					can.style.height = h - headHight - pos.h + "px";
					sig.resize(w, h - headHight - pos.h);
					//sig.redraw(0);
				} else {
					can.style.width = h - 0 + "px";
					can.style.height = w - headHight - pos.h + "px";
					sig.resize(h, w - headHight - pos.h);
					//sig.redraw(0);
				}
			}, false);
			api.addEventListener({
				name : 'ChangeUserName'
			}, function(ret, err) {
				userName = ret.value || userName;
			});
			// 监听是否注册或者登录成功
			api.addEventListener({
				name : 'reg_login_successEvent'
			}, function(ret) {
				if (ret && ret.value) {
					userName = ret.value.sig;
					uid = $api.getStorage("uid");
				}
			});			
			// 监听新增用户魔板数据状态
			api.addEventListener({
				name : 'AddSign'
			}, function(ret) {
				//alert(JSON.stringify(ret));
				if (ret) {
					console.log(ret.value);					
				}
			});				
			
			api.readFile({
		                path:"fs://template.json"
	                },function(ret,err){
	                	if(ret)
	                	{
	                		tpllist=JSON.parse(ret.data);
	                		//alert(JSON.stringify(tpllist));
	                	}	                	
	            });
		};
	</script>
</html>